//
//  settingTableViewController.m
//  ZPSALE
//
//  Created by tmosport on 14-7-2.
//  Copyright (c) 2014年 tmosport. All rights reserved.
//

#import "settingTableViewController.h"
#import "feedBackViewController.h"
#import "SDImageCache.h"

@interface settingTableViewController ()
@property (strong, nonatomic) IBOutlet UISwitch *cleanCacheSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *wifiSetSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *timeSetSwitch;
- (IBAction)timeSetAction:(id)sender;
- (IBAction)cleanCache:(UISwitch *)sender;
- (IBAction)wifiSetAction:(UISwitch *)sender;
@property (strong, nonatomic) IBOutlet UIButton *logoutButton;

@end

@implementation settingTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设置中心";
    [self setNavigationItem];
    self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc]
                                            initWithTitle:@" "
                                            style:UIBarButtonItemStyleBordered
                                            target:nil
                                            action:nil];
    [self setupFooterView];
}

-(void)setNavigationItem {
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setupFooterView
{
    [_logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [_logoutButton.layer setCornerRadius:4.0];
    [_logoutButton setBackgroundColor:ColorFromRGB(0xff0100)];
}
- (void)logout
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"确认退出当前账号？"
                                                            delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil];
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
        NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
        [dataDic setValue:@"" forKey:@"username"];
        [dataDic setValue:@"" forKey:@"passwd"];
        [dataDic setValue:@"" forKey:@"memberId"];
        [dataDic setValue:@"" forKey:@"token"];
        [dataDic writeToFile:homePath atomically:YES];
        [CheckLoginObject saveLoginWithStatus:NO];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeNumberOfProductInCart" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        //去评价
        if (indexPath.row == 1) {
            NSString *appID = @"906938080";
            NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
            [[UIApplication sharedApplication] openURL: [NSURL URLWithString:str]];
        }
        //新版本监测
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)timeSetAction:(id)sender {
}

- (IBAction)cleanCache:(UISwitch *)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"清空缓存"
                                                        message:@"确定要清除吗"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
    [alertView show];
}

- (IBAction)wifiSetAction:(UISwitch *)sender {
}
//AlertView的协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (alertView.tag) {
        case 1:
        {
            if (buttonIndex == 1)
            {
                NSString *appID = @"904826337";
                NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
                [[UIApplication sharedApplication] openURL: [NSURL URLWithString:str]];
            }
        }
            break;
        case 2:
        {
            //do nothing..
        }
            break;
        default:
        {
            if (buttonIndex == 1)
            {
                //清除缓存
                [[SDImageCache sharedImageCache] clearDisk];
                _cleanCacheSwitch.enabled = NO;
            }else{
                _cleanCacheSwitch.On = YES;
            }
            
        }
            break;
    }
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    feedBackViewController *destVC = segue.destinationViewController;
    destVC.memberId  = _memberId;
    destVC.token    = _token;
    destVC.hidesBottomBarWhenPushed = YES;
}
@end